package com.qclix.inv.xml;

import com.qclix.inv.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Basic Implementation of DomaintoXml and XmlToDomain translation
 */
public class DfltObjectTranslatorImpl implements DomainToXMLTranslator, XMLtoDomainTranslator {

    // TODO use a library to map the beans

    public XMLInvoice getXMLversion(Invoice invoice) {
        XMLInvoice xmlVersion = new XMLInvoice();
        xmlVersion.setIssueDate(invoice.getIssueDate());
        xmlVersion.setNumber(invoice.getNumber());
        xmlVersion.setClient(getXMLversion(invoice.getClient()));
        XMLInvoiceLine xmlLine;
        int idx = 0;
        for (InvoiceLine line: invoice.getLines()) {
            xmlLine = getXMLversion(line);
            xmlLine.setIdx(idx++);
            xmlVersion.getLines().add(xmlLine);
        }
        return xmlVersion;
    }

    public XMLInvoiceLine getXMLversion(InvoiceLine line) {
        XMLInvoiceLine xmlVersion = new XMLInvoiceLine();
        xmlVersion.setId(line.getId());
        xmlVersion.setDescription(line.getDescription());
        xmlVersion.setQuantity(line.getQuantity());
        xmlVersion.setUnitPrice(line.getUnitPrice());
        xmlVersion.setVat(line.getVat());
        return xmlVersion;
    }

    public XMLClient getXMLversion(Client client) {
        XMLClient xmlVersion = new XMLClient();
        xmlVersion.setId(client.getId());
        xmlVersion.setName(client.getName());
        xmlVersion.setStreet1(client.getStreet1());
        xmlVersion.setStreet2(client.getStreet2());
        xmlVersion.setZip(client.getZip());
        xmlVersion.setCity(client.getCity());
        xmlVersion.setCountry(client.getCountry());
        xmlVersion.setVat(client.getVat());
        return xmlVersion;
    }

    public XMLWeekDay getXMLversion(WeekDay day) {
        XMLWeekDay xmlVersion = new XMLWeekDay();
        xmlVersion.setDate(new Date(day.getDate().getTime()));
        xmlVersion.setHoliday(day.isHoliday());
        xmlVersion.setWeekend(day.isWeekend());
        xmlVersion.setWorkedStdHours(day.getWorkedStdHours());
        xmlVersion.setWorkedOverTimeHours(day.getWorkedOverTimeHours());
        xmlVersion.setPartOfInvoice(day.isPartOfInvoice());
        return xmlVersion;
    }
    
    public XMLTimesheet getXMLversion(Timesheet timesheet) {
        XMLTimesheet xmlVersion = new XMLTimesheet();
        xmlVersion.setId(timesheet.getId());
        xmlVersion.setClientName(timesheet.getClientName());
        xmlVersion.setProjectName(timesheet.getProjectName());
        xmlVersion.setConsultant(timesheet.getConsultant());
        xmlVersion.setResponsiblePerson(timesheet.getResponsiblePerson());
        for (WeekDay weekDay: timesheet.getDays()) {
            xmlVersion.getDays().add(getXMLversion(weekDay));
        }
        return xmlVersion;
    }

    public Invoice getDomainVersion(XMLInvoice invoice) {
        Invoice domainVersion = new Invoice();
        domainVersion.setIssueDate(invoice.getIssueDate());
        domainVersion.setNumber(invoice.getNumber());
        domainVersion.setClient(getDomainVersion(invoice.getClient()));
        List<XMLInvoiceLine> xmlLines = invoice.getLines();
        Collections.sort(invoice.getLines(),new LineIndexComparator());
        for (XMLInvoiceLine line: xmlLines) {
            domainVersion.getLines().add(getDomainVersion(line));
        }
        return domainVersion;
    }

    public InvoiceLine getDomainVersion(XMLInvoiceLine line) {
        InvoiceLine domainVersion = new InvoiceLine(line.getDescription(), line.getQuantity(), line.getUnitPrice(), line.getVat());
        domainVersion.setId(line.getId());
        return domainVersion;
    }

    public Client getDomainVersion(XMLClient client) {
        Client domainVersion = new Client();
        domainVersion.setId(client.getId());
        domainVersion.setName(client.getName());
        domainVersion.setStreet1(client.getStreet1());
        domainVersion.setStreet2(client.getStreet2());
        domainVersion.setZip(client.getZip());
        domainVersion.setCity(client.getCity());
        domainVersion.setCountry(client.getCountry());
        domainVersion.setVat(client.getVat());
        return domainVersion;
    }

    public WeekDay getDomainVersion(XMLWeekDay day) {
        WeekDay domainVersion = new WeekDay();
        domainVersion.setDate(new Date(day.getDate().getTime()));
        domainVersion.setHoliday(day.isHoliday());
        domainVersion.setWeekend(day.isWeekend());
        domainVersion.setWorkedStdHours(day.getWorkedStdHours());
        domainVersion.setWorkedOverTimeHours(day.getWorkedOverTimeHours());
        return domainVersion;
    }

    public Timesheet getDomainVersion(XMLTimesheet timesheet) {
        Timesheet domainVersion = new Timesheet();
        domainVersion.setId(timesheet.getId());
        domainVersion.setClientName(timesheet.getClientName());
        domainVersion.setProjectName(timesheet.getProjectName());
        domainVersion.setConsultant(timesheet.getConsultant());
        domainVersion.setResponsiblePerson(timesheet.getResponsiblePerson());
        for (XMLWeekDay xmlWeekDay: timesheet.getDays()) {
            domainVersion.getDays().add(getDomainVersion(xmlWeekDay));
        }
        return domainVersion;
    }
}
